const App = Vue.createApp({
    data() {
        return {
            Data: [], type: -1,
        }
    }, methods: {
        BatchSetting(cid) {
            layer.open({
                title: "温馨提示",
                content: '请问您需要进行什么操作？',
                icon: 3,
                btn: ['批量替换商品图片', '将商品图片批量本地化', '取消'],
                btn1: function () {
                    App.ReplaceProductImages(cid);
                },
                btn2: function () {
                    App.BatchProductPictureTransfer(cid);
                }
            })
        },
        pitch_on() {
            var amount = [];
            $("#table input[type='checkbox']").each(function () {
                if ($(this).is(":checked")) {
                    if ($(this).val() != 'true' && $(this).val() != '') amount.push($(this).val());
                }
            });
            return amount;
        }, ReplaceProductImages(arr, images = false) { //批量换图
            let value = '';
            if (images) {
                value = images.join("\n");
            }
            layer.prompt({
                title: '批量换图,每行一个图片链接',
                formType: 2,
                value: value
            }, function (value, index, elem) {
                if (value === '') {
                    layer.msg('请输入图片链接！');
                    return;
                }
                let images = value.split("\n");
                if (images.length === 0) {
                    layer.msg('请输入图片链接！');
                    return;
                }
                layer.close(index);
                let is = layer.msg('操作中，请稍后...', { icon: 16, time: 9999999 });
                $.ajax({
                    type: "POST", url: 'main.php?act=ReplaceProductImages', data: {
                        array: arr, image: images
                    }, dataType: "json", success: function (res) {
                        //重新获取商品列表
                        layer.close(is);
                        if (res.code === 1) {
                            layer.alert(res.msg, {
                                icon: 1,
                                btn1: function () {
                                    App.ClassList();
                                }
                            });
                        } else {
                            layer.alert(res.msg, {
                                icon: 2
                            });
                        }
                    }
                })
            });
        },
        BatchEditor(e) {
            const arr = this.pitch_on();
            if (arr.length === 0) {
                layer.alert('请至少选择一个商品!', { icon: 2 });
                return;
            }

            switch (e) {
                case 12:
                    App.ReplaceClasstImages(arr);
                    break;
                case 13: //批量换图
                    App.ReplaceProductImages(arr);
                    break;
                case 14:
                    layer.alert('是否将这' + arr.length + '个分类删除？', {
                        title: '商品批量删除', icon: 3, btn: ['确认', '取消'], btn1: function () {
                            App.ajax_state(4, arr);
                        }
                    });
                    break;
            }
        },
        ajax_state(type, arr, cid = -1) {
            var index = layer.msg('正在调整中...', {
                icon: 16, time: 9999999
            });
            $.ajax({
                type: "post", url: "ajax.php?act=class_State_all", data: {
                    type: type, arr: arr, cid: cid
                }, dataType: "json", success: function (data) {
                    if (data.code > 0) {
                        layer.alert(data.msg, {
                            icon: 1, end: function (layero, index) {
                                App.ClassList();
                            }
                        });
                    } else {
                        layer.msg(data.msg, {
                            icon: 2
                        });
                    }
                }, error: function () {
                    layer.alert('修改失败！');
                    layer.close(index);
                }
            });
        }
        , select_id(id) {
            if ($('.box_' + id).prop('checked') == true) {
                $('.box_' + id).attr("checked", true);
                $('.box_' + id).prop("checked", true);
            } else {
                $('.box_' + id + ',#list_all').attr('checked', false);
                $('.box_' + id + ',#list_all').prop("checked", false);
            }
        },
        select_all() {
            console.log('选中')
            if ($('#list_all').prop('checked') == true) {
                $(".list_box,#list_all").attr("checked", true);
                $(".list_box,#list_all").prop("checked", true);
            } else {
                $(".list_box,#list_all").attr('checked', false);
                $(".list_box,#list_all").prop("checked", false);
            }
        },
        BatchProductPictureTransfer(cid) { //图片本地化
            //确认框
            layer.confirm('是否要将这个分类下的全部商品图片本地化？<hr>当您将商品图片本地化后，其他同行将无法根据图片地址信息找到上游货源，并且再也不会出现图片无法显示的问题。然而，请注意，如果您的主机或服务器带宽较小，可能会导致图片加载缓慢，从而影响用户体验。', {
                title: '商品图片本地化', icon: 3, area: ["300px"], btn: ['确认', '取消'], btn1: function () {
                    let is = layer.msg('操作中，请稍后...', { icon: 16, time: 9999999 });
                    $.ajax({
                        type: "POST", url: './main.php?act=BatchProductPictureTransfer', data: {
                            array: [cid], type: 2
                        }, dataType: "json", success: function (res) {
                            layer.close(is);
                            if (res.code === 1) {
                                layer.alert(res.msg, {
                                    icon: 1
                                });
                            } else {
                                layer.alert(res.msg, {
                                    icon: 2
                                });
                            }
                        }
                    })
                }
            })
        }, ReplaceClasstImages(cid) { //批量换图
            layer.prompt({
                title: '批量换图 ',
                formType: 3,
            }, function (value, index, elem) {
                if (value === '') {
                    layer.msg('请输入图片链接！');
                    return;
                }

                layer.close(index);
                let is = layer.msg('操作中，请稍后...', { icon: 16, time: 9999999 });
                $.ajax({
                    type: "POST", url: './main.php?act=ReplaceClasstImages', data: {
                        cid: cid, image: value
                    }, dataType: "json", success: function (res) {
                        layer.close(is);
                        if (res.code === 1) {
                            layer.alert(res.msg, {
                                icon: 1
                            });
                        } else {
                            layer.alert(res.msg, {
                                icon: 2
                            });
                        }
                    }
                })
            });
        },
        DeleteClass() {
            //清理所有分类
            layer.confirm('您确定清理所有分类吗？', {
                icon: 3,
                btn: ['确定', '取消'],
                btn1: function () {
                    let is = layer.msg('清理中，请稍后...', { icon: 16, time: 9999999 });
                    $.ajax({
                        type: "POST",
                        url: './main.php?act=DeleteClass',
                        dataType: "json",
                        success: function (res) {
                            layer.close(is);
                            if (res.code == 1) {
                                App.ClassList();
                            } else {
                                layer.alert(res.msg, {
                                    icon: 2
                                });
                            }
                        },
                        error: function () {
                            layer.msg('服务器异常！');
                        }
                    });
                }
            })
        },
        ResetSort() {
            //重置排序
            layer.confirm('确定要重置排序吗，重置后排序ID全部会变成0？', {
                btn: ['确定', '取消'],
                btn1: function () {
                    let is = layer.msg('重置中，请稍后...', { icon: 16, time: 9999999 });
                    $.ajax({
                        type: "POST",
                        url: './main.php?act=ClassSortReset',
                        dataType: "json",
                        success: function (res) {
                            layer.close(is);
                            if (res.code == 1) {
                                App.ClassList();
                            } else {
                                layer.alert(res.msg, {
                                    icon: 2
                                });
                            }
                        },
                        error: function () {
                            layer.msg('服务器异常！');
                        }
                    });
                }
            })
        },
        Deployment(cid, SuborderNumber) {
            let id = $("#Tab_" + cid);
            if (id.text() === '展开(' + SuborderNumber + ')') {
                id.attr('class', 'layui-icon layui-icon-down');
                $(".Tab_" + cid).show(200);
                id.text('合上(' + SuborderNumber + ')');
            } else {
                id.attr('class', 'layui-icon layui-icon-right');
                $(".Tab_" + cid).hide(200);
                id.text('展开(' + SuborderNumber + ')');
            }
        },
        ClassPaySet(cid, index) {
            let is = layer.msg('加载中，请稍后...', { icon: 16, time: 9999999 });
            $.ajax({
                type: "POST", url: './main.php?act=ClassPaySet', data: {
                    cid: cid, key: index,
                }, dataType: "json", success: function (res) {
                    layer.close(is);
                    if (res.code == 1) {
                        App.ClassList();
                    } else {
                        layer.alert(res.msg, {
                            icon: 2
                        });
                    }
                }, error: function () {
                    layer.msg('服务器异常！');
                }
            });
        }, ClassDelete(cid, name) {
            let content = `
<div class="layui-form">
  <input type="checkbox" value="1" name="ClassType"  title="当前分类" lay-skin="tag" checked>
  <input type="checkbox" value="2" name="ClassType" title="下级分类" lay-skin="tag"> 
  <input type="checkbox" value="3" name="ClassType" title="当前分类商品" lay-skin="tag"> 
  <input type="checkbox" value="4" name="ClassType" title="下级分类商品" lay-skin="tag"> 
</div>
<blockquote class="layui-elem-quote" style="margin-top: 1em">
  注意事项：<br>分类和商品删除后无法恢复，请谨慎操作！
  <br>当一级分类删除后，如果未删除下级分类，则下级分类会自动升级为一级分类！<br>
  如果商品失去了上级，则商品分类ID默认为-1，即为未分类！，可以在商品列表单独查看！
</blockquote>
            `;
            layer.open({
                title: '请选择需要删除的内容 - ' + name,
                content: content,
                btn: ['确认删除', '取消'],
                btn1: function () {
                    //获取选中的值
                    let type = [];
                    $("input:checkbox[name='ClassType']:checked").each(function () {
                        type.push($(this).val());
                    })
                    if (type.length === 0) {
                        layer.msg('请至少选择一项', { icon: 2 });
                        return;
                    }
                    App.ClassDelGet(cid, name, type);
                },
                success: function () {
                    layui.use('form', function () {
                        let form = layui.form;
                        form.render();
                    });
                }
            });
        },
        ClassDelGet(cid, name, type) {
            let is = layer.msg('删除中，请稍后...', { icon: 16, time: 9999999 });
            $.ajax({
                type: "POST", url: './main.php?act=ClassDelete', data: {
                    cid: cid, name: name, type: type,
                }, dataType: "json", success: function (res) {
                    layer.close(is);
                    if (res.code == 1) {
                        layer.alert(res.msg, {
                            icon: 1, btn1: function () {
                                App.ClassList();
                            }
                        });
                    } else {
                        layer.alert(res.msg, {
                            icon: 2
                        });
                    }
                }, error: function () {
                    layer.msg('服务器异常！');
                }
            });
        },
        SortSet(cid, _this) {
            let value = _this.value - 0;
            let is = layer.msg('加载中，请稍后...', { icon: 16, time: 9999999 });
            $.ajax({
                type: "POST", url: './main.php?act=ClassSortSet', data: {
                    cid: cid, sort: value,
                }, dataType: "json", success: function (res) {
                    layer.close(is);
                    if (res.code == 1) {
                        App.ClassList();
                    } else {
                        layer.alert(res.msg, {
                            icon: 2
                        });
                    }
                }
            });
        }
        , ClassStateSet(cid, type, name) {
            let is = layer.msg('加载中，请稍后...', { icon: 16, time: 9999999 });
            $.ajax({
                type: "POST", url: './main.php?act=ClassStateSet', data: {
                    cid: cid, type: type, name: name
                }, dataType: "json", success: function (res) {
                    layer.close(is);
                    if (res.code == 1) {
                        App.ClassList();
                    } else {
                        layer.alert(res.msg, {
                            icon: 2
                        });
                    }
                }, error: function () {
                    layer.msg('服务器异常！');
                }
            });
        }, ClassList() {
            let is = layer.msg('分类载入中，请稍后...', { icon: 16, time: 9999999 });
            $.ajax({
                type: "POST", url: './main.php?act=ClassList', data: {
                    type: 2
                }, dataType: "json", success: function (res) {
                    layer.close(is);
                    App.type = 1;
                    if (res.code == 1) {
                        App.Data = res.data;
                        App.type = 1;
                    } else {
                        App.Data = [];
                    }
                }, error: function () {
                    layer.msg('服务器异常！');
                }
            });
        }
    }
}).mount('#App');
App.ClassList();